

var mongoose = require('mongoose')

var attractionSchema = mongoose.Schema({
   name: String,
   note: String,
   location: { lat: Number, lng: Number },
   history: {
       event: String,
       note: String,
       email: String,
       date: Date,
   },
   updateId: String,
   approved: Boolean

});






var attractionModel = mongoose.model('Attraction', attractionSchema);
module.exports = attractionModel;



// GET /api/attractions
// GET /api/attractions/:id
// POST /api/attraction
// PUT  /api/attraction/:id
// DEL /api/attraction/:id
// POST /users  用来创建新用户；

// GET /users 用来获取用户列表；

// GET /users/:id 用来获取单个用户；

// PUT /users/:id 用来更新用户信息；

// DELETE /users/:id 用来删除用户；

var attractionModel = require('../attractionModel');

var express = require('express');
var router = express.Router();
var fs = require('fs');
var path = require('path');


router.get('/api/attractions',function(req,res){

    attractionModel.find({approved: true},function(err,attractions){
        if(err){
            return res.status(500).json({
                code:0,
                msg:'server err'
            });
        }

        res.json(attractions.map(function(item){

            return {
                name: item.name,
                id: item._id,
                note: item.note,
                location: item.location,
            }
        }));
    })
});

router.get('/api/attractions/:id',function(req,res){

    attractionModel.findById(req.params.id,function(err,attractions){
        if(err){
            return res.status(500).json({
                code:0,
                msg:'server err'
            });
        }

        res.json({
            code: 0,
            dat: attractions
        });
    })
});


router.post('/api/attractions',function(req,res){

    var newModel = new attractionModel({

        name: req.body.name,
        note: req.body.note,
        location: { lat: req.body.lat, lng: req.body.lng },
        history: {
            event: 'created',
            email: req.body.email,
            date: new Date(),
        },
        approved: false,
    });

    newModel.save(function(err,data){
        if(err){
            return res.status(500).json({
                code:0,
                msg:'server err'
            });
        }

        res.json({
            code: 0,
            dat: data
        });
    })
})


// jwt  session
// https://www.jianshu.com/p/89f5305386eb
// https://www.jianshu.com/p/a7882080c541